package com.ossjk.oa.accounting.controller;


import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.oa.accounting.entity.CwSalary;
import com.ossjk.oa.accounting.service.ICwSalaryService;
import com.ossjk.oa.attendance.vo.SalaryAttVo;
import com.ossjk.oa.system.service.IDepartmentService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@RestController
@RequestMapping("/accounting/cwSalary")
public class CwSalaryController extends BaseController {


	@Autowired
	private ICwSalaryService iCwSalaryService;
	@Autowired
	private IDepartmentService  iDepartmentService ;
	
	
	//@RequiresPermissions("0000")
	@RequestMapping("/list")
	public Object list(Page page) {

	   	Wrapper wrapper = Condition.create();
	   	//  .isWhere(true).orderBy("e.crtm", true);

		page = iCwSalaryService.selectPage(page, wrapper);
		return responseSuccess(page);

	}

	@PostMapping("/insert")
	public Object insert(CwSalary cw_salary) {
		Wrapper wrapper = Condition.create();

		if (iCwSalaryService.insert(cw_salary)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}


	@GetMapping("/toUpdate/{id}")
	public Object toUpdate(@PathVariable("id") String id) {
		return responseSuccess(iCwSalaryService.selectById(id));
	}

 
	@PutMapping("/update")
	public Object update(CwSalary cw_salary) {
		if (iCwSalaryService.updateById(cw_salary)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iCwSalaryService.deleteBatchIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

///////////////////////////////////////////////////////////////////////////
	
	    //判断能否生成考勤
		@GetMapping("/canGenKq")
		public Object canGenKaoqin(String mth, String eid, HttpServletRequest req) {
			//  当前月
		    try {
				if( CommonMethod.isoFormat.parse(mth+"-01").getTime()  - CommonMethod.isoFormat.parse(CommonMethod.calMth(0)+"-01").getTime()>=1  ) {
					   return responseFail("不可以生成未结束的月份.");
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//  查询该员工该月有无考勤
			if(iCwSalaryService.selectSalaryAttvByEid(eid, mth)!=null) {
			   return responseFail("不可以重复生成.");
			}
			//计算新的考勤
			return responseSuccess();
		}
	
	//生成考勤报告
	@GetMapping("/genKq")
	public Object genKaoqin(String mth, String eid, HttpServletRequest req) {
		 
		//  查询该员工该月有无考勤
		SalaryAttVo svo= iCwSalaryService.selectSalaryAttvByEid(eid, mth);
		if(svo!=null) {
		   return responseFail("不可以重复生成.");
		}
		
		//计算新的考勤
		
		
		 
		
		
		
		return responseFail();
		 
		
	}
	
	
	@GetMapping("/listKq")
	public Object listKaoqin(String mth, String did, String eid, HttpServletRequest req) {
		
		 if(mth == null) {   
			 mth = CommonMethod.calMth(-1);  //默认上一月
		 }
		 // 计算 【上一月】
		 String preMth = CommonMethod.calMth(mth, -1);
		 String nxtMth =null;
		 //如果不是当月计算下一月	 
		 if(mth != CommonMethod.calMth(0) ) {
			 nxtMth = CommonMethod.calMth(mth, 1); 
		 }
		 
		if(eid != null) {   //查询个人
		 
			
		}else {   //查询部门
			// Session作用域放一个list<部门>
			if (req.getSession().getAttribute("depts") == null) {
				req.getSession().setAttribute("depts", iDepartmentService.selectList(null) );
			} 
			
            if(mth == null) {   	
		 
			
			   // nxtMth=staffBiz.countKaoqinByMth(nxtMth) > 0L ? nxtMth : null;
			} 			
				if (did == null ) {
					// 全部部門
					did = "0";
				}	
		}
		return responseSuccess(  null    );
	  }
	 

}

